1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package com.google.common.primitives;
18
19 import com.google.common.annotations.GwtCompatible;
20 import com.google.common.annotations.GwtIncompatible;
21 import com.google.common.collect.testing.Helpers;
22 import com.google.common.testing.NullPointerTester;
23 import com.google.common.testing.SerializableTester;
24
25 import junit.framework.TestCase;
26
27 import java.util.Arrays;
28 import java.util.Comparator;
29 import java.util.List;
30
31
32
33
34
35
36 @GwtCompatible(emulated = true)
37 @SuppressWarnings("cast")
38 public class SignedBytesTest extends TestCase {
39 private static final byte[] EMPTY = {};
40 private static final byte[] ARRAY1 = {(byte) 1};
41
42 private static final byte LEAST = Byte.MIN_VALUE;
43 private static final byte GREATEST = Byte.MAX_VALUE;
44
45 private static final byte[] VALUES =
46 {LEAST, -1, 0, 1, GREATEST};
47
48 public void testCheckedCast() {
49 for (byte value : VALUES) {
50 assertEquals(value, SignedBytes.checkedCast((long) value));
51 }
52 assertCastFails(GREATEST + 1L);
53 assertCastFails(LEAST - 1L);
54 assertCastFails(Long.MAX_VALUE);
55 assertCastFails(Long.MIN_VALUE);
56 }
57
58 public void testSaturatedCast() {
59 for (byte value : VALUES) {
60 assertEquals(value, SignedBytes.saturatedCast((long) value));
61 }
62 assertEquals(GREATEST, SignedBytes.saturatedCast(GREATEST + 1L));
63 assertEquals(LEAST, SignedBytes.saturatedCast(LEAST - 1L));
64 assertEquals(GREATEST, SignedBytes.saturatedCast(Long.MAX_VALUE));
65 assertEquals(LEAST, SignedBytes.saturatedCast(Long.MIN_VALUE));
66 }
67
68 private static void assertCastFails(long value) {
69 try {
70 SignedBytes.checkedCast(value);
71 fail("Cast to byte should have failed: " + value);
72 } catch (IllegalArgumentException ex) {
73 assertTrue(value + " not found in exception text: " + ex.getMessage(),
74 ex.getMessage().contains(String.valueOf(value)));
75 }
76 }
77
78 public void testCompare() {
79 for (byte x : VALUES) {
80 for (byte y : VALUES) {
81
82 int expected = Byte.valueOf(x).compareTo(y);
83 int actual = SignedBytes.compare(x, y);
84 if (expected == 0) {
85 assertEquals(x + ", " + y, expected, actual);
86 } else if (expected < 0) {
87 assertTrue(x + ", " + y + " (expected: " + expected + ", actual" + actual + ")",
88 actual < 0);
89 } else {
90 assertTrue(x + ", " + y + " (expected: " + expected + ", actual" + actual + ")",
91 actual > 0);
92 }
93 }
94 }
95 }
96
97 public void testMax_noArgs() {
98 try {
99 SignedBytes.max();
100 fail();
101 } catch (IllegalArgumentException expected) {
102 }
103 }
104
105 public void testMax() {
106 assertEquals(LEAST, SignedBytes.max(LEAST));
107 assertEquals(GREATEST, SignedBytes.max(GREATEST));
108 assertEquals((byte) 127, SignedBytes.max(
109 (byte) 0, (byte) -128, (byte) -1, (byte) 127, (byte) 1));
110 }
111
112 public void testMin_noArgs() {
113 try {
114 SignedBytes.min();
115 fail();
116 } catch (IllegalArgumentException expected) {
117 }
118 }
119
120 public void testMin() {
121 assertEquals(LEAST, SignedBytes.min(LEAST));
122 assertEquals(GREATEST, SignedBytes.min(GREATEST));
123 assertEquals((byte) -128, SignedBytes.min(
124 (byte) 0, (byte) -128, (byte) -1, (byte) 127, (byte) 1));
125 }
126
127 public void testJoin() {
128 assertEquals("", SignedBytes.join(",", EMPTY));
129 assertEquals("1", SignedBytes.join(",", ARRAY1));
130 assertEquals("1,2", SignedBytes.join(",", (byte) 1, (byte) 2));
131 assertEquals("123", SignedBytes.join("", (byte) 1, (byte) 2, (byte) 3));
132 assertEquals("-128,-1", SignedBytes.join(",", (byte) -128, (byte) -1));
133 }
134
135 public void testLexicographicalComparator() {
136 List<byte[]> ordered = Arrays.asList(
137 new byte[] {},
138 new byte[] {LEAST},
139 new byte[] {LEAST, LEAST},
140 new byte[] {LEAST, (byte) 1},
141 new byte[] {(byte) 1},
142 new byte[] {(byte) 1, LEAST},
143 new byte[] {GREATEST, GREATEST - (byte) 1},
144 new byte[] {GREATEST, GREATEST},
145 new byte[] {GREATEST, GREATEST, GREATEST});
146
147 Comparator<byte[]> comparator = SignedBytes.lexicographicalComparator();
148 Helpers.testComparator(comparator, ordered);
149 }
150
151 @GwtIncompatible("SerializableTester")
152 public void testLexicographicalComparatorSerializable() {
153 Comparator<byte[]> comparator = SignedBytes.lexicographicalComparator();
154 assertSame(comparator, SerializableTester.reserialize(comparator));
155 }
156
157 @GwtIncompatible("NullPointerTester")
158 public void testNulls() {
159 new NullPointerTester().testAllPublicStaticMethods(SignedBytes.class);
160 }
161 }